﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTABAUDIT : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        DataSet setDISPO;
        double FLeng = 0, GLeng = 0, WLeng = 0;
        int FLeadTime = 0, GLeadTime = 0, WLeadTime = 0, YLeadTime = 14;
        double Fnum = 0, Gnum = 0, Wnum = 0;
        string FEnd = "", GEnd = "", WEnd = "", YEnd = "";
        string DISPONUM = "";
        DataRow lastrow = null;
        public bool IsAB05
        {
            get
            {
                if (ChkAB05.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool IsAB55
        {
            get
            {
                if (ChkAB55.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool IsAB45
        {
            get
            {
                if (ChkAB45.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public bool IsAB50
        {
            get
            {
                if (ChkAB50.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        
        JLTABAUDITData m_Data, m_DataX;
        JLTABAUDITReport m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        string UserID = "";
        CollectionViewSource vwTABAX
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwTABAX");
            }
        }
        DataRow dwTABAX
        {
            get
            {
                if (vwTABAX.View == null)
                    return null;
                if ((DataRowView)vwTABAX.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTABAX.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwAPKOPF
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwAPKOPF");
            }
        }
        DataRow dwAPKOPF
        {
            get
            {
                if (vwAPKOPF.View == null)
                    return null;
                if ((DataRowView)vwAPKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwAPKOPF.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwAPZUORD
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwAPZUORD");
            }
        }
        DataRow dwAPZUORD
        {
            get
            {
                if (vwAPZUORD.View == null)
                    return null;
                if ((DataRowView)vwAPZUORD.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwAPZUORD.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABAX
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwABAX");
            }
        }
        DataRow dwABAX
        {
            get
            {
                if (vwABAX.View == null)
                    return null;
                if ((DataRowView)vwABAX.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABAX.View.CurrentItem).Row;
            }
        }
        string KRS
        {
            get
            {
                return m_Data.Left(txtVKPAUFKRS.Text.Trim() + "  ", 2);
            }
            set
            {
                txtVKPAUFKRS.Text = m_Data.Left(txtVKPAUFKRS.Text.Trim() + value, 2);
            }
        }
        string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFLFD.Text.Trim(), 6);
            }
            set
            {
                txtVKPAUFLFD.Text = m_Data.Right("000000" + value, 6); ;
            }
        }
        string POS
        {
            get
            {
                return m_Data.Right("000" + txtVKPAUFPOS.Text.Trim(), 3);
            }
            set
            {
                txtVKPAUFPOS.Text = m_Data.Right("000" + value, 3);
            }
        }   
     
        BindingListCollectionView cvABA
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgABAX.ItemsSource);
            }
        }
        string filterABA="";
        public JLTABAUDIT()
        {
            InitializeComponent();
            this.Name = "计划编排"; 
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsLoaded == true)
                return;
            vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged;
            vwABAX.View.CurrentChanged += vwABAX_CurrentChanged;
            vwAPZUORD.View.CurrentChanged += vwAPZUORD_CurrentChanged;
            IsLoaded = true;
            //EventDispatcher.Instance.OnMessage += new EventHandler<MessageEventArgs>(_LogExplorerDock.OnMessage);
        }

        void vwABAX_CurrentChanged(object sender, EventArgs e)
        {
            if (dwABAX == null)
                return;
            string filter = "ABAWAKZ='" + dwABAX["ABAWAKZ"].ToString() + "' AND ABAART='" + dwABAX["ABAART"].ToString() + "' AND ABAFARBE='" + dwABAX["ABAFARBE"].ToString() + "'";
            DataRow[] rows = SetData.ICC_ABAX.Select(filter);
            DataTable tb = rows.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            SetProd.Clear();
            SetProd.Merge(tb);
            double qty = 0;
            string dd = "";
            foreach (DataRow row in tb.Rows)
            {
                qty += Convert.ToDouble(row["ABARESMG"].ToString()) / 100;
                dd = row["ABAVKENDD"].ToString();
            }
            txtTQty.Text = qty.ToString();
            txtDD.Text = dd;
            vwABAX.View.MoveCurrentTo(-1);
            if (vwTABAX.View != null)
                vwTABAX.View.MoveCurrentToFirst();
        }

        void vwAPZUORD_CurrentChanged(object sender, EventArgs e)
        {
            if (dwAPZUORD == null)
                return;
            string APZSTUFE = dwAPZUORD["APZSTUFE"].ToString();
            string filter = "APKSTUFE='" + APZSTUFE + "'";
            //cvAPK.CustomFilter = filter;
        }
        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwVKPOS == null)
                    return;
                if (lastrow == dwVKPOS)
                    return;
                lastrow = dwVKPOS;
               
                DataSet SResult;//=m_Data.PrePlan(dwVKPOS,DAB05,DAB55,DAB45,DAB50);
                //SetProd.Merge(SResult);
                if (vwABPOS.View != null)
                {
                    vwABPOS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"]; 
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                m_Data = new JLTABAUDITData(OracleDB, proxy); 
                txtUserID.Text = (string)dicParamater["User"];
                txtPassword.Password = (string)dicParamater["Pwd"];
                m_Data.UserID = Helpers.Helper.Left((string)dicParamater["User"] + "           ", 8);
                KRS = "J4";
                LFD = "000003";
                ReloadCon();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count > 0)
            {
                DataRow dwUser = setResult.Tables[0].Rows[0];
                txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
                UserID = userID;
                m_Data.UserID = userID;
                this.UserID = userID;

                m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                    + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
                m_Data.MANDANT = Mandant;
                m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "已登录";
                txtLogIn.Background = new SolidColorBrush(Colors.Green);
                expLog.IsExpanded = false; 
            }
        }

        #endregion 

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.Clear();
                DataSet SResult = new DataSet();
                DataRow dwData = SetData.VKPOS.NewRow();
                dwData["VKPMANDANT"] = "00";
                dwData["VKPAUFKRS"] = KRS;
                dwData["VKPAUFLFD"] = LFD;
                SResult = m_Data.LoadData(dwData);
                foreach(DataTable tb in SResult.Tables)
                {
                    string tbname = tb.TableName;
                    SetData.Merge(tb);
                }
                string filterABA = "";
                if (IsAB05)
                {
                    filterABA += "ABAWAKZ='F' AND";
                }
                if (IsAB55)
                {
                    filterABA += " ABAWAKZ='R' AND";
                }
                if (IsAB45)
                {
                    filterABA += " ABAWAKZ='B' AND";
                }
                if (IsAB50)
                {
                    filterABA += " ABAWAKZ='G' AND";
                }
                filterABA = filterABA + "***";
                filterABA = filterABA.Replace("AND***", "");
                cvABA.CustomFilter = filterABA;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ReloadCon()
        {
            DataSet SResult = null;
            SResult = m_Data.LoadEnv();
            SetEnv.Merge(SResult);
        }
        private void btnInsertt_Click(object sender, RoutedEventArgs e)
        {
            SetProd.VKPOS.Clear();
            SetProd.VKPOS.Merge(SetData.VKPOS);
            SetProd.APFOLGE.Clear();
            SetProd.APFOLGE.Merge(SetData.APFOLGE);
            SetProd.APARTK.Clear();
            SetProd.APARTK.Merge(SetData.APARTK);
         
        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = null;
                //SResult = m_Data.PrePlan(dwVKPOS,DAB05,DAB55,DAB45,DAB50);
                SetProd.Clear();
                SetProd.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnDeleteABON_Click(object sender, RoutedEventArgs e)
        {
         
        }
        private void txtVKPAUFLFD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }

        private void btnAddAPZ_Click(object sender, RoutedEventArgs e)
        {
            if (dwAPKOPF == null)
            {
                MessageBox.Show("请选择一个流程");
                return;
            }
            if (dwAPZUORD == null)
            {
                MessageBox.Show("请选择一个物料");
                return;
            }
            dwAPZUORD["APZANRKRS"]=dwAPKOPF["APKANRKRS"];
            dwAPZUORD["APZALFDNR"]=dwAPKOPF["APKALFDNR"];
            m_Data.INTEXDBC.SaveAPZUORD(dwAPZUORD,null);
        }

        private void btnSaveAPZ_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnCombine_Click(object sender, RoutedEventArgs e)
        {

            if (dwTABAX == null)
            {
                MessageBox.Show("请先选择要合并的生产单");
            }
            double RQTY=Convert.ToDouble(txtTQty.Text.ToString());
            m_Data.Combine(SetProd, RQTY,txtDD.Text.ToString());
        }
    }
}
